#
# Make upgrade.bin
#
#

# PLATFORM is generated when "make unpack"
# So we dont need to specify BOARD param when "make pack"
include ../Makefile.param

# Max size 15 bytes
ifeq ($(PLATFORM), HI3521A)
PLATFORM_KEY = HB3521A
else
ifeq ($(PLATFORM), HI3520D_V300)
PLATFORM_KEY = HB3520D_V300
else
ifeq ($(PLATFORM), HI3520D)
PLATFORM_KEY = HB3520D_NVR
else
ifeq ($(PLATFORM), HI3535)
PLATFORM_KEY = HB3535_NVR
else
ifeq ($(PLATFORM), HI3536)
PLATFORM_KEY = HB3536_NVR
else
ifeq ($(PLATFORM), HI3531A)
PLATFORM_KEY = HB3531A
else
ifeq ($(PLATFORM), HI3531)
PLATFORM_KEY = HB3531_NVR
else
ifeq ($(PLATFORM), HI3516C)
PLATFORM_KEY = HB3516C_IPC
else
@$(ECHO) "***Error: PLATFORM is $(PLATFORM)"
endif
endif
endif
endif
endif
endif
endif
endif

ifeq ($(OEM_KEY_STR_EXT), )
KEY_VALUE = $(PLATFORM_KEY)
else
KEY_VALUE = $(PLATFORM_KEY)_$(OEM_KEY_STR_EXT)
endif
$(warning "KEY_VALUE = $(KEY_VALUE)")

SWVER_STR	= "$(shell cat $(OUTPUT_USR_FS)/swver | grep build)"
SVNVER_STR	= "$(shell cat $(OUTPUT_USR_FS)/swver | grep SVN)"

GUI_LANG_TOOL	= ./gui_lang_tool
UPGRADE_MAKER	= ./upgrade_maker
UPGRADE_UNMAKER	= ./upgrade_unmaker
RESTORE_MAKER	= ./restore_maker
MK_CRAMFS		= mkfs.cramfs
MK_JFFS2		= mkfs.jffs2
ifeq ($(PLATFORM), HI3535)
MK_YAFFS2		= mkyaffs2image504
else
ifeq ($(PLATFORM), HI3531A)
MK_YAFFS2		= mkyaffs2image100
else
ifeq ($(PLATFORM), HI3536)
MK_YAFFS2		= mkyaffs2image610
else
ifeq ($(PLATFORM), HI3520D_V300)
MK_YAFFS2		= mkyaffs2image100
else
ifeq ($(PLATFORM), HI3521A)
MK_YAFFS2		= mkyaffs2image100
else
MK_YAFFS2		= mkyaffs2image
endif
endif
endif
endif
endif

ifeq ($(SEC_YAFFS2_ENABLE), true)
YAFFS2_SEC_EXT_NAME = GD
endif

UPGRADE_MAKER_OUTPUT_NAME	= upgrade_make.bin
UPGRADE_FILE_NAME			= upgrade.bin
RESTORE_FILE_NAME			= restore.bin
RESUME_FILE_NAME			= resume.bin
OUTPUT_TAR_FILE_NAME		= output.tar.gz
SYS_CRAMFS_NAME				= sys.hbgk.cramfs
USR_CRAMFS_NAME				= usr.hbgk.cramfs
WEB_CRAMFS_NAME				= web.hbgk.cramfs
YDT_BOX_CRAMFS_NAME			= ydt_box.cramfs
TMP_MOUNT_DIR				= /tmp/upgrade_usr_cramfs
SLAVE_ROOT_UNPACK			= $(SLAVE_OUTPUT_DIR)/root
TAR_BIN_WITH_HEADER			= tar.bin
UNPACK_LOG					= unpack.log

# Flash Type defines
RM_FLT_NOR			=	0
RM_FLT_NAND			=	1
RM_FLT_SPI_NOR		=	2
RM_FLT_SPI_NAND		=	3

# Part Type defines
RM_PT_BIN			=	0
RM_PT_CRAMFS		=	1
RM_PT_YAFFS2		=	2
RM_PT_JFFS2			=	3

# File Type defines
RM_FT_ERASURE		=	0
RM_FT_EXTRACT		=	1
RM_FT_OVERLAY		=	2
RM_FT_HDMAKER		=	3

# Many broken symbolic link exist in root partition
# *MUST* specify make -L option to work around it
export MAKEOVERRIDES 	+= -L
export MAKEFLAGS		+= -L

# Disable build in rules, fix himd.l depends on himd.c problem
.SUFFIXES: 

.PHONY: clean unpack unpack_clean pack unpack_help unpack_restore permission check_shared_folders

ifeq ($(SEC_YAFFS2_ENABLE), true)
all: $(UPGRADE_FILE_NAME) $(RESTORE_FILE_NAME) $(RESTORE_FILE_NAME)_$(YAFFS2_SEC_EXT_NAME) $(RESUME_FILE_NAME) $(RESUME_FILE_NAME)_$(YAFFS2_SEC_EXT_NAME)
else
all: $(UPGRADE_FILE_NAME) $(RESTORE_FILE_NAME) $(RESUME_FILE_NAME)
endif

# define UPGRADE_MAKER param
ifeq ($(SMALL_FLASH_SIZE), true)
UM_UBOOT	= 40
UM_KERNEL	= 41
UM_ROOT_FS	= 42
UM_ARCHIVE	= 5
UM_CONCAT	= 9
else
ifeq ($(SINGLE_NOR_FLASH), true)
$(warning "here, Single nor flash no need set upgrade maker parameters")
else
ifeq ($(SINGLE_NAND_FLASH), true)
$(warning "here, Single nand flash no need set upgrade maker parameters")
else
UM_UBOOT	= 40
UM_KERNEL	= 41
UM_ROOT_FS	= 12
UM_USR_FS	= 13
UM_ARCHIVE	= 5
UM_CONCAT	= 9
endif
endif
endif

# No need to depend on key which is indirect dpended already
ifeq ($(SMALL_FLASH_SIZE), true)
UPGRADE_FILE_NAME_DEP = uboot.hbgk.bin kernel.hbgk.bin root.hbgk.bin usr.hbgk.bin $(TAR_BIN_WITH_HEADER)
RESTORE_FILE_NAME_DEP = uboot.bin uImage root.hbgk.cramfs usr.hbgk.cramfs web.hbgk.cramfs cfg.hbgk.jffs2 logo.hbgk.jffs2
RESUME_FILE_NAME_DEP = uboot.bin uImage root.hbgk.cramfs usr.hbgk.cramfs web.hbgk.cramfs cfg.hbgk.jffs2 logo.hbgk.jffs2
else
ifeq ($(SINGLE_NOR_FLASH), true)
ALL_FILE_NAME_DEP = uboot.hbgk.bin env.hbgk.bin logo.hbgk.bin kernel.hbgk.bin root.hbgk.bin usr.hbgk.bin web.hbgk.bin
UPGRADE_FILE_NAME_DEP = $(ALL_FILE_NAME_DEP) lang.hbgk.bin
RESTORE_FILE_NAME_DEP = $(ALL_FILE_NAME_DEP) usr1.hbgk.bin
RESUME_FILE_NAME_DEP = $(RESTORE_FILE_NAME_DEP) backup.hbgk.bin
else
ifeq ($(SINGLE_NAND_FLASH), true)
ifeq ($(DUAL_CORE_CPU_MODE), true)
ALL_FILE_NAME_DEP = uboot.hbgk.bin env.hbgk.bin logo.hbgk.bin kernel.hbgk.bin root.hbgk.bin slave_arm_uboot.hbgk.bin slave_arm_kernel.hbgk.bin slave_arm_root.hbgk.bin
else
ALL_FILE_NAME_DEP = uboot.hbgk.bin env.hbgk.bin logo.hbgk.bin kernel.hbgk.bin root.hbgk.bin
endif
UPGRADE_FILE_NAME_DEP = $(ALL_FILE_NAME_DEP) usr.hbgk.bin lang.hbgk.bin
ifeq ($(BUILD_MINI_UBOOT), true)
RESTORE_FILE_NAME_DEP = mini_boot.hbgk.bin $(ALL_FILE_NAME_DEP) usr0.hbgk.bin usr1.hbgk.bin
ifeq ($(SEC_YAFFS2_ENABLE), true)
RESTORE_FILE_NAME_DEP_SEC = mini_boot.hbgk.bin $(ALL_FILE_NAME_DEP) usr0.hbgk.bin_$(YAFFS2_SEC_EXT_NAME) usr1.hbgk.bin_$(YAFFS2_SEC_EXT_NAME)
endif
else
RESTORE_FILE_NAME_DEP = $(ALL_FILE_NAME_DEP) usr0.hbgk.bin usr1.hbgk.bin
ifeq ($(SEC_YAFFS2_ENABLE), true)
RESTORE_FILE_NAME_DEP_SEC = $(ALL_FILE_NAME_DEP) usr0.hbgk.bin_$(YAFFS2_SEC_EXT_NAME) usr1.hbgk.bin_$(YAFFS2_SEC_EXT_NAME)
endif
endif
RESUME_FILE_NAME_DEP = $(RESTORE_FILE_NAME_DEP) backup.hbgk.bin
ifeq ($(SEC_YAFFS2_ENABLE), true)
RESUME_FILE_NAME_DEP_SEC = $(RESTORE_FILE_NAME_DEP_SEC) backup.hbgk.bin_$(YAFFS2_SEC_EXT_NAME)
endif
else
UPGRADE_FILE_NAME_DEP = uboot.hbgk.bin kernel.hbgk.bin root.hbgk.bin usr.hbgk.bin
RESTORE_FILE_NAME_DEP = 4M-nor.bin root.hbgk.cramfs usr.hbgk.yaffs2 cfg.hbgk.yaffs2
RESUME_FILE_NAME_DEP = 4M-nor.bin root.hbgk.cramfs usr.hbgk.yaffs2 cfg.hbgk.yaffs2
endif
endif
endif

$(UPGRADE_FILE_NAME): $(UPGRADE_FILE_NAME_DEP)
	@$(ECHO) [$@] Changed file:$?
	$(ECHO) $(SWVER_STR) $(SVNVER_STR)
	$(UPGRADE_MAKER) -f $(RM_FT_HDMAKER) -b $(SWVER_STR) -r $(SVNVER_STR) -l $^
	mv $(UPGRADE_MAKER_OUTPUT_NAME) $@

root.hbgk.cramfs: $(shell find $(OUTPUT_ROOT_FS) 2>/dev/null)
	@$(ECHO) [$@] Changed file:$?
	$(MK_CRAMFS) $(OUTPUT_ROOT_FS) $@

$(GUI_LANG_TAR): $(shell find $(GUI_LANG_SPLITED) 2>/dev/null)
	@$(ECHO) [$@] Changed file:$?
	@$(MKDIR) -p $(GUI_LANG_MERGED)
	@$(GUI_LANG_TOOL) -m $(GUI_LANG_SPLITED) $(GUI_LANG_MERGED)
	@$(MKDIR) -p $(OUTPUT_CFG_LOGO)
	# Since we changed to sub directory, we need to use ../$@ as dest path
	cd $(GUI_LANG_MERGED) && tar czvf ../$@ lang

$(CFG_DIR_NAME)/logo/key: key
	$(MKDIR) -p $(CFG_DIR_NAME)/logo/
	$(CP) key $@

# Update key afer $(UPGRADE_MAKER) changed.
# this will force target made by $(UPGRADE_MAKER) re-made themself -
# when new version $(UPGRADE_MAKER) commited 
key: Makefile $(UPGRADE_MAKER)
	@$(ECHO) [$@] Changed file:$?
	$(CHMOD) 755 $(UPGRADE_MAKER)
	$(CHMOD) 755 $(RESTORE_MAKER)
	$(UPGRADE_MAKER) -s $(KEY_VALUE)

permission:
	@if [[ -f $(UPGRADE_MAKER) ]]; then		\
		$(CHMOD) 755 $(UPGRADE_MAKER);	\
	fi
	@if [[ -f $(UPGRADE_UNMAKER) ]]; then		\
		$(CHMOD) 755 $(UPGRADE_UNMAKER);	\
	fi
	@if [[ -f $(RESTORE_MAKER) ]]; then		\
		$(CHMOD) 755 $(RESTORE_MAKER);	\
	fi
	@if [[ -f $(GUI_LANG_TOOL) ]]; then		\
		$(CHMOD) 755 $(GUI_LANG_TOOL);	\
	fi
	-@$(CHMOD) a+x $(SCRIPTS_DIR)/*.sh

upc: unpack_clean
unpack_clean:
	@$(RM) uboot.bin uImage
	@$(RM) uboot.hbgk.bin kernel.hbgk.bin 4M-nor.bin
	@$(RM) usr $(HB_BK_DIR_NAME) $(CFG_DIR_NAME)
	@$(RM) *.cramfs *.jffs2
	@$(RM) usr.hbgk.bin root.hbgk.bin $(TAR_BIN_WITH_HEADER)
	@$(RM) mnt mnt.tar -Rf
	@$(RM) key $(CFG_DIR_NAME)/logo/key
	@$(RM) $(SLAVE_ROOT_UNPACK)
	@$(RM) $(OUTPUT_HB_BK)
	@$(RM) $(OUTPUT_CFG)
	@$(RM) $(UNPACK_LOG)
	@$(RM) $(SLAVE_CHIP_DIR)/scripts
	@$(RM) $(SLAVE_OUTPUT_DIR)/$(SLAVE_ROOT_FS_CRAMFS)
	@$(RM) $(SLAVE_OUTPUT_DIR)/$(SLAVE_ROOT_FS_IMG)
	@$(RM) $(GUI_LANG_MERGED) $(GUI_LANG_SPLITED) -Rf

clean: distclean
	$(RM) $(MAKE_LOG_FILE)
	$(RM) $(OUTPUT_DIR)/$(OUTPUT_HB_BK)/ 
	$(RM) *.cramfs
	$(RM) $(HB_BK_DIR_NAME)/$(WEB_CRAMFS_NAME)
	$(RM) *.yaffs2
	$(RM) usr.hbgk.bin root.hbgk.bin $(UPGRADE_FILE_NAME) $(RESTORE_FILE_NAME)
	$(RM) key $(CFG_DIR_NAME)/logo/key
	$(RM) uboot.hbgk.bin kernel.hbgk.bin 4M-nor.bin

distclean:
	$(RM) `ls -a | awk '{ if (($$0 != ".") && ($$0 != "..") && ($$0 != ".svn") && ($$0 != "Makefile")) print $$0 }'`
	
h:
	@$(ECHO) "Short cut for pack/unpack targets:"
	@$(ECHO) "* make unpack [up]"
	@$(ECHO) "* make pack [p]"
	@$(ECHO) "* make unpack_clean [upc]"
	@$(ECHO) "* make unpack_restore [upr]"
	
unpack_help:
	@$(ECHO) "1. make unpack"
	@$(ECHO) "2. Change whatever you want"
	@$(ECHO) "3. make pack"

#.host:/ on /mnt/hgfs type vmhgfs (rw,ttl=1)
check_shared_folders:
	@if [[ `pwd | grep "hgfs"` ]]; then	\
		$(ECHO) "Cant execute this because you are now under windows share folder:/mnt/hgfs";	\
		$(ECHO) "Please excute this in linux native file system";	\
		exit 1;	\
	fi

$(RESTORE_FILE_NAME): $(RESTORE_FILE_NAME_DEP)
	@$(ECHO) [$@] Changed file:$?
	@ls $^ -hl
	$(UPGRADE_MAKER) -f $(RM_FT_HDMAKER) -b $(SWVER_STR) -r $(SVNVER_STR) -l $^
	mv $(UPGRADE_MAKER_OUTPUT_NAME) $@

ifeq ($(SEC_YAFFS2_ENABLE), true)
$(RESTORE_FILE_NAME)_$(YAFFS2_SEC_EXT_NAME): $(RESTORE_FILE_NAME_DEP_SEC)
	@$(ECHO) [$@] Changed file:$?
	@ls $^ -hl
	$(UPGRADE_MAKER) -f $(RM_FT_HDMAKER) -b $(SWVER_STR) -r $(SVNVER_STR) -l $^
	mv $(UPGRADE_MAKER_OUTPUT_NAME) $@
endif

$(RESUME_FILE_NAME): $(RESUME_FILE_NAME_DEP)
	@$(ECHO) [$@] Changed file:$?
	@ls $^ -hl
	$(UPGRADE_MAKER) -f $(RM_FT_HDMAKER) -b $(SWVER_STR) -r $(SVNVER_STR) -l $^
	mv $(UPGRADE_MAKER_OUTPUT_NAME) $@

ifeq ($(SEC_YAFFS2_ENABLE), true)
$(RESUME_FILE_NAME)_$(YAFFS2_SEC_EXT_NAME): $(RESUME_FILE_NAME_DEP_SEC)
	@$(ECHO) [$@] Changed file:$?
	@ls $^ -hl
	$(UPGRADE_MAKER) -f $(RM_FT_HDMAKER) -b $(SWVER_STR) -r $(SVNVER_STR) -l $^
	mv $(UPGRADE_MAKER_OUTPUT_NAME) $@
endif

$(OUTPUT_TAR_FILE_NAME):
	$(RM) $(TOP_DIR)/$(OUTPUT_TAR_FILE_NAME)
	tar -zcf $(TOP_DIR)/$(OUTPUT_TAR_FILE_NAME) $(OUTPUT_DIR)

check_board:
ifeq ($(BOARD),)
	@$(ECHO) "***Error: BOARD is not set"
	@$(EXIT) -1
endif

upr: unpack
unpack_restore: permission
	$(RESTORE_MAKER) -k $(RESTORE_FILE_NAME)

up: unpack
unpack: check_board permission check_shared_folders
	@if [[ -f ./usr || -d ./usr ]]; then											\
		$(ECHO) "**** \"usr\" already exist, please delete or rename it";			\
		ls usr -hl;																	\
		$(ECHO) "**** You can try make unpack_clean to remove all files unpacked";	\
		exit 1;																		\
	fi
	$(UPGRADE_UNMAKER) $(UPGRADE_FILE_NAME)

	@if [[ -f ./$(HB_BK_DIR_NAME).tar.gz ]]; then	\
		$(ECHO) "$(HB_BK_DIR_NAME).tar.gz exist";	\
		tar xzvf $(HB_BK_DIR_NAME).tar.gz;		\
	fi

	@if [[ -f ./$(CFG_DIR_NAME).tar.gz ]]; then	\
		$(ECHO) "$(CFG_DIR_NAME).tar.gz exist";	\
		tar xzvf $(CFG_DIR_NAME).tar.gz;		\
	fi
	
	$(MKDIR) -p $(TMP_MOUNT_DIR);
	sudo mount -o loop -t cramfs rootfs.hbgk.cramfs $(TMP_MOUNT_DIR);
	$(CP) -r $(TMP_MOUNT_DIR) ./root;
	sudo umount $(TMP_MOUNT_DIR);

	@if [[ -f ./$(USR_CRAMFS_NAME) ]]; then													\
		sudo mount -o loop -t cramfs $(USR_CRAMFS_NAME) $(TMP_MOUNT_DIR);					\
	else																					\
		sudo mount -o loop -t cramfs $(HB_BK_DIR_NAME)/$(USR_CRAMFS_NAME) $(TMP_MOUNT_DIR);	\
	fi
	$(CP) -r $(TMP_MOUNT_DIR) ./usr;
	sudo umount $(TMP_MOUNT_DIR);

	@if [[ -f $(WEB_CRAMFS_NAME) ]]; then													\
		sudo mount -o loop -t cramfs $(WEB_CRAMFS_NAME) $(TMP_MOUNT_DIR);					\
	else																					\
		sudo mount -o loop -t cramfs $(HB_BK_DIR_NAME)/$(WEB_CRAMFS_NAME) $(TMP_MOUNT_DIR);	\
	fi
	$(CP) -r $(TMP_MOUNT_DIR) ./web;
	sudo umount $(TMP_MOUNT_DIR);
	$(RM) $(TMP_MOUNT_DIR);
	
	$(MKDIR) -p $(GUI_LANG_MERGED);
	$(MKDIR) -p $(GUI_LANG_SPLITED)/config/;
	tar xzvf $(CFG_DIR_NAME)/logo/$(GUI_LANG_TAR_NAME) -C $(GUI_LANG_MERGED);
	$(GUI_LANG_TOOL) -d $(GUI_LANG_MERGED) $(GUI_LANG_SPLITED)/config/;
	
ifeq ($(SMALL_FLASH_SIZE), true)
$(warning "Small flash")
include $(SMALL_FLASH_MK)
else
ifeq ($(SINGLE_NOR_FLASH), true)
$(warning "Single Nor flash")
include $(SINGLE_NOR_FLASH_MK)
else
ifeq ($(SINGLE_NAND_FLASH), true)
$(warning "Single Nand flash")
include $(SINGLE_NAND_FLASH_MK)
else
$(warning "Full flash")
include $(Full_FLASH_MK)
endif
endif
endif

p: pack
pack: check_board $(UPGRADE_FILE_NAME)

include $(BUILD_MK)

# vim:noet:sw=4:ts=4
